home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DN114_3D.ZIP / BC_EX1.C next >
C/C++ Source or Header  |  1996-02-03  |  4KB  |  152 lines

  1. /*
  2.  
  3.   "Introduction to 3D Programming" Tutorial series
  4.   Article #1 - For DemoNews 114
  5.  
  6.   Sample code for Borland C 3.1 or above (might work with lesser versions,
  7.   but it's untested below 3.1 so I can't say for sure).
  8.  
  9.   Make sure at least 286 instructions are enabled in your compiler options.
  10.  
  11. */
  12.  
  13. #include <stdlib.h>
  14. #include <conio.h>
  15.  
  16.  
  17. /* "vector" structure - holds a 3D cartesian point, and its projection. */
  18. typedef struct vector
  19. {
  20. int x;
  21. int y;
  22. int z;
  23. int scrx;
  24. int scry;
  25. } vector;
  26.  
  27. /* Box object, used for the example. */
  28.  
  29. vector Box[8] =  {
  30.          { 20, 40, 30, 0,0},
  31.          { 20, 40,-30, 0,0},
  32.          { 20,-40, 30, 0,0},
  33.          { 20,-40,-30, 0,0},
  34.          {-20, 40, 30, 0,0},
  35.          {-20, 40,-30, 0,0},
  36.          {-20,-40, 30, 0,0},
  37.          {-20,-40,-30, 0,0}
  38.          };
  39.  
  40. /*
  41.  
  42. Videomode and Putpixel functions for graphics display
  43.  
  44. (Yes, the putpixel is not optimized, but that's not the point of
  45.  this example program :-)
  46.  
  47. */
  48.  
  49. void videomode(unsigned int mode)
  50. {
  51.   asm {
  52.     mov ax, mode;
  53.     int 10h;
  54.       }
  55. }
  56.  
  57. void putpixel(unsigned int x, unsigned int y, char color)
  58. {
  59.   asm {
  60.     mov ax, 0a000h;
  61.     mov es, ax;
  62.     mov ax, y;
  63.     shl ax, 6;
  64.     mov bx, ax;
  65.     shl ax, 2;
  66.     add ax, bx;
  67.     add ax, x;
  68.     mov di, ax;
  69.     mov al, color;
  70.     mov [es:di], al;
  71.       }
  72. }
  73.  
  74.  
  75. /* Function Declarations for PointProject and DrawBox */
  76.  
  77. void PointProject(int distance, vector point, vector *dest, vector center);
  78. void DrawBox(int cenx, int ceny, int cenz, char color);
  79.  
  80.  
  81. /*
  82. Main program - Draws three boxes at different 3D centers, showing how
  83.            the depth perception works.
  84. */
  85.  
  86. void main(void)
  87. {
  88.   videomode(0x13);
  89.  
  90.   DrawBox(-60, 0, -40, 13);  /* Purple box, Z=-40 so it's further away */
  91.   DrawBox(  0, 0,   0, 14);  /* Yellow box, Z=0 so it's at the image plane */
  92.   DrawBox( 60, 0,  40, 15);  /* White box,  Z=40 so it's closer to you */
  93.  
  94.   while (!kbhit());
  95.   videomode(0x03);
  96. }
  97.  
  98.  
  99. /*
  100.  
  101. void PointProject(int distance, vector point, vector *dest, vector center)
  102.  
  103. Takes a viewing distance from the origin along the Z axis (in our examples
  104. we've been using 256, so that's what I used in the main program), and the
  105. 3D point you want to project.  Fills in the 2D scrx and scry values of the
  106. given vector, or a separate "after projection" vector, depending on what
  107. you pass in *dest (I used the same vector for this example).
  108.  
  109. The center vector parameter is just added to the point you give.  Stupid
  110. by itself, yes... but if you use the same center for the set of points in
  111. an object (I used a box in this program), the whole object is moved (called
  112. "translation") by the center.  Just run the example, and you'll see what I
  113. mean. :-)
  114.  
  115. */
  116.  
  117.  
  118. void PointProject(int distance, vector point, vector *dest, vector center)
  119. {
  120.   dest->scrx = (256*(point.x+center.x) / (distance-(point.z+center.z))) + 160;
  121.   dest->scry = 100 - (256*(point.y+center.y) / (distance-(point.z+center.z)));
  122. }
  123.  
  124. /*
  125.  
  126. void DrawBox(int cenx, int ceny, int cenz, char color)
  127.  
  128. Draws the Box object with the 3D center you give.  The center points are
  129. merged into the one "center" vector that PointProject wants.  All the points
  130. use the same center, which "moves" the object.
  131.  
  132. */
  133.  
  134. void DrawBox(int cenx, int ceny, int cenz, char color)
  135. {
  136.   int count;
  137.   vector BoxCenter;
  138.  
  139.   BoxCenter.x = cenx;
  140.   BoxCenter.y = ceny;
  141.   BoxCenter.z = cenz;
  142.  
  143.   for (count=0; count<8; count++)
  144.   {
  145.     PointProject(256, Box[count], &Box[count], BoxCenter);
  146.     putpixel(Box[count].scrx, Box[count].scry, color);
  147.   }
  148. }
  149.  
  150.  
  151.  
  152.